package com.wrh.demo.controller;

import com.wrh.demo.bean.User;
import com.wrh.demo.service.TestService;
import com.wrh.springboot.restwrap.RestWrapIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wrh
 */
@RestController
@RequestMapping("test")
public class TestController {


    @GetMapping("getTest1")
    public Object getTest1() {
        return "返回一段字符串";
    }

    @RestWrapIgnore
    @GetMapping("getTest2")
    public Object getTest2(String user, String pwd) {
        return "user：" + user + "，pwd：" + pwd;
    }

    @GetMapping("getTest3")
    public Object getTest3(String user, @RequestParam("cities") List<String> cities) {
        return "user：" + user + "，pwd：" + cities.toString();
    }

    @PostMapping("getTest4")
    public Object getTest4(User user) {
        return user.toString();
    }

    @PostMapping("getTest5")
    public Object getTest5(@RequestBody User user) {
        return user.toString();
    }

    @GetMapping("getTest6")
    public Object getTest6() throws Exception {
        throw new Exception("抛出异常");
    }

    @GetMapping("getTest7")
    public Object getTest7() throws FileNotFoundException {
        throw new FileNotFoundException("找不到格点文件");
    }

    @GetMapping("getTest8")
    public Object getTest8(LocalDateTime time) {
        return time.toString();
    }

    @Autowired
    private TestService testService;

    @RestWrapIgnore
    @GetMapping("getTest9")
    public void getTest9() throws Exception {
        testService.throwException();
    }

    @GetMapping("getTest10")
    public ResponseEntity<byte[]> getTest10(HttpServletResponse response) throws Exception {
        InputStream png = this.getClass().getClassLoader().getResourceAsStream("public/长江流域降水预报图.png");
        BufferedImage image = ImageIO.read(png);
        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "png", byteOutputStream);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
                .body(byteOutputStream.toByteArray());
    }
}
